﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using UMFramework.Network;

namespace Hi_Baidu.Net
{
    public abstract class MessageListener : IListener
    {
        public MessageListener(int ReadLength)
        {
            this.ReadLength = ReadLength;
        }
        public Network_MessagePool NetworkMessagePool = new Network_MessagePool();
        protected bool IS_LOOP_GET_MESSAGE = true;
        protected Thread CurrentThread = null;
        private Socket userSocket = null;
        /// <summary>
        ///     网络流读取数组大小
        /// </summary>
        protected int ReadLength = 3072;

        /// <summary>
        ///     要监听的用户基础套接字
        /// </summary>
        public Socket UserSocket
        {
            get { return userSocket; }
            set { userSocket = value; }
        }
        #region 'OnGetMessage' event definition code

        /// <summary>
        ///     EventArgs derived type which holds the custom event fields
        /// </summary>
        public class OnGetMessageEventArgs : System.EventArgs
        {
            /// <summary>
            ///     Use this constructor to initialize the event arguments
            ///     object with the custom event fields
            /// </summary>
            public OnGetMessageEventArgs(String strContent, Socket socUserSocket)
            {
                this.Content = strContent;
                this.UserSocket = socUserSocket;
            }

            /// <summary>
            ///     TODO: Describe the purpose of this event argument here
            /// </summary>
            public readonly String Content;

            /// <summary>
            ///     TODO: Describe the purpose of this event argument here
            /// </summary>
            public readonly Socket UserSocket;

        }

        // Private delegate linked list (explicitly defined)
        private EventHandler<OnGetMessageEventArgs> OnGetMessageEventHandlerDelegate;

        /// <summary>
        ///     TODO: Describe the purpose of this event here
        /// </summary>
        public event EventHandler<OnGetMessageEventArgs> OnGetMessage
        {
            // Explicit event definition with accessor methods
            add
            {
                OnGetMessageEventHandlerDelegate = (EventHandler<OnGetMessageEventArgs>)Delegate.Combine(OnGetMessageEventHandlerDelegate, value);
            }
            remove
            {
                OnGetMessageEventHandlerDelegate = (EventHandler<OnGetMessageEventArgs>)Delegate.Remove(OnGetMessageEventHandlerDelegate, value);
            }
        }

        /// <summary>
        ///     This is the method that is responsible for notifying
        ///     receivers that the event occurred
        /// </summary>
        protected virtual void OnOnGetMessage(OnGetMessageEventArgs e)
        {
            if (OnGetMessageEventHandlerDelegate != null)
            {
                OnGetMessageEventHandlerDelegate(this, e);
            }
        }

        #endregion //('OnGetMessage' event definition code)
        /// <summary>
        ///     循环监听指定用户基础套接字
        /// </summary>
        protected abstract void Listen();


        #region IListener 成员

        public bool Start()
        {
            IS_LOOP_GET_MESSAGE = true;
            Init();
            return true;
        }

        public bool Stop()
        {
            if (IS_LOOP_GET_MESSAGE)
            {
                IS_LOOP_GET_MESSAGE = false;
                this.Dispose();
            }
            return true;
        }

        public void ChanageState(bool State)
        {
            IS_LOOP_GET_MESSAGE = State;
        }

        public abstract void Init();

        public abstract void Dispose();

        public Thread GetCurrentThread()
        {
            return this.CurrentThread;
        }

        #endregion

        #region IMetaData 成员

        private String key;
        public string Key
        {
            get
            {
                return key;
            }
            set
            {
                key = value;
            }
        }
        #endregion
    }
}
